<?php
require_once( 'need.inc.php' );
$user_id = Login::NeedLogin();


if ( isset( $_POST) ){
//处理修改
	$str_change = null;
	if ( $_POST['t'] == 1 ) {
		$user = Table::fetch( $table['user'] , $user_id );
		if ( $_POST['pw'] == $user['password']  ){
			if ( $_POST['pw1'] == $_POST['pw2'] and $_POST['pw1'] != '' ){
				if ( DB::Update( $table['user'] ,$user_id , array( 'password'=> $_POST['pw1'] ) )){
					$str_change = '修改成功';
				}else{
					$str_change = '修改失败';
				}
			}else{
				$str_change = '两次新密码不一样';
			}
		}else{
			$str_change = '旧密码不正确';
		}
	}


	//处理新建
	$str_new = null;
	if ( $_POST['t'] == 2 ){
		$name  = empty( $_POST['name'] ) ? NULL : $_POST['name'];
		$pw1   = empty( $_POST['pw1'] ) ? NULL : $_POST['pw1'];
		$pw2   = empty( $_POST['pw2'] ) ? NULL : $_POST['pw2'];
		$email = empty( $_POST['email'] ) ? NULL : $_POST['email'];
		if ( $name and $pw1 and $pw2  and $email ){
			if ( $pw1 == $pw2 ){
				if ( DB::Insert( $table['user'] , array( 'name'=> $name , 
								'password' => $pw1 ,
								'email' => $email 
								)
					       )
				   ){
					$str_new = ' 新建成功';
				}else{
					$str_new = '新建失败';
				}
			}else{
				$str_new = '两次输入密码不一致';
			}
		}else{
			$str_new = ' 数据不完整';
		}
	}
}

//用于修改自己的密码
$user = Table::fetch( $table['user'] , $user_id );

//管理其它帐号
if ( $user_id == 1)
	$users_list = DB::GetQueryResult( "SELECT * FROM `".$table['user']."` WHERE `id` != 1", false);
else
	$users_list = null;


//print_r ( $user );
//print_r ( $users_list);



Template::Display( 'user_setting', array(
			'user' => $user,
			'users_list' => $users_list,

			'str_change' => $str_change,
			'str_new' => $str_new,
			));


?>
